$(function() {

	var page = 1;
	var tddata = [{
		key: 'number',
		type: 1,
		url: ''
	}, {
		key: 'driver',
		type: 2,
		url: '{:url("detail")}',
		id: 137
	}, {
		key: 'phone',
		type: 1,
		url: ''
	}, {
		key: 'company',
		type: 1,
		url: ''
	}, {
		key: 'time',
		type: 1,
		url: ''
	}, {
		key: 'addr',
		type: 1,
		url: ''
	}, {
		key: 'state2',
		type: 1,
		url: ''
	}];
	var addoperation = `<td>
						<div class="tpl-table-black-operation operate">
							<a href="javascript:;" class='detail'>
								<i class="am-icon-pencil"></i> 查看
							</a>
							<a href="javascript:;" class='exit'>
								<i class="am-icon-pencil"></i> 编辑
							</a>
						</div>
					</td>`
	ajaxaccident()

	function ajaxaccident() {
		Load()
		$.ajax({
			type: 'get',
			data: {},
			url: '{$static_dir}/assets/json/accident.json',
			success: function(res) {
				Load(1)
//				res = JSON.parse(res)
				console.log(res)
				for(var stateidx = 0; stateidx < res.data.length; stateidx++) {
					var dispose;
					if(res.data[stateidx].state == 1) {
						res.data[stateidx].state2 = '待处理'
						dispose = `<a href="javascript:;">
								<i class="am-icon-pencil"></i> 处理
							</a>`
					} else if(res.data[stateidx].state == 2) {
						res.data[stateidx].state2 = '已处理'
						dispose = " "
					}
					$('.operate').eq(stateidx).append(dispose)
				}
				$('tbody').html(tbodymainf2(res.data, 1, tddata, addoperation, 'order'))
				$('.pagination').html(pagechang1(res.data.length, 10, page))
				for(var disposeidx = 0; disposeidx < res.data.length; disposeidx++) {
					var dispose;
					if(res.data[disposeidx].state == 1) {
						dispose = `<a href="javascript:;" class='dispose'>
								<i class="am-icon-pencil"></i> 处理
							</a>`
					} else if(res.data[disposeidx].state == 2) {
						dispose = ""
					}
					$('.operate').eq(disposeidx).append(dispose)
				}
				$('.laquo').on('click', function() {
					page -= 1;
					console.log(page)
					ajaxaccident(page)
				})
				$('.pageli').on('click', function() {
					page = $(this).html()
					ajaxaccident(page)
				})
				//下一页
				$('.raquo').on('click', function() {
					page += 1;
					ajaxaccident(page)
				});
				$('.car_rental').on('click', function() {
					choosecar('{:url("rent/index")}?op=search')
				});
				$('.detail').on('click', function() {
					layer.open({
						type: 2,
						title: '事故详情',
						shadeClose: true,
						shade: 0.8,
						area: ['30%', '80%'],
						content: '{:url("accidentdetail")}?op=search' //iframe的url
					});
				})
				$('.exit').on('click', function() {
					window.location.href = '{:url("accidentadd")}?id=' + $('this').closest('tr').data('id')
				})
				$('.dispose').on('click', function() {
					disposeopen(1)
				})
			}
		})
	}

	function disposeopen(id){
		console.log(id)
		var load = layer.load(0, {
			shade: [0.3, '#fff']
		});

		layer.close(load);
		var index = layer.open({
			title: false,
			type: 1,
			skin: 'layui-layer-rim', //加上边框
			area: ['550px', '300px'], //宽高
			content: `<div class="am-u-sm-12" style="padding:20px">
						<form class="am-form-inline tpl-form-border-form" enctype="multipart/form-data" data-ajax="1" data-check="1">
						<div class="am-u-sm-8 cl-p " >
							<div class='am-u-sm-12 margin_bottom_10'>
								<label class="am-u-sm-4 am-form-label cl-p" style="line-height: 35px;font-size: 14px;">损失： </label>
								
								<div class="am-u-sm-8 cl-p">
									<input type="text" name="number" value="{$item.number}" class="tpl-form-input" style="line-height: 21px;" data-check='require' data-msg=""/>
								</div>
							</div>
							<div class='am-u-sm-12'>
							<label class="am-u-sm-4 am-form-label cl-p" style="line-height: 35px;font-size: 14px;">处理结果说明： </label>
								<div class='am-u-sm-8 cl-p'>
									<textarea class="cl-p" style='height:150px'></textarea>
								</div>
							</div>
							<div class='am-u-sm-12' style='text-align:center;margin-top:5px'>
								<button type='button' class='am-btn-sm am-btn am-btn-primary sum'>提交</button>
								<button type='button' class='am-btn-sm am-btn am-btn-primary close'>取消</button>
							</div>
						</div>
						<div class='am-u-sm-4 cl-p'>
							<div class="am-u-sm-12">
								<div class="am-u-sm-12 ">
									<div class="am-form-group file-w">
										<div style="width: 150px;height: 150px;">
											<img class="fileimg" data-magnify="1" src="{if $item.driving_license_img2}{:CxImg($item.driving_license_img2)}{else/}{$static_dir}/assets/img/noimg.gif{/if}" style="width: 100%;height: 100%;" />
										</div>
										<button type="button" data-type='file' class="am-btn am-btn-danger am-btn-sm "><i class="am-icon-cloud-upload"></i> 添加处理凭证</button>
										<input type="file" name="driving_license_img2" data-type='img' style="display: none;">
									</div>
								</div>
							</div>
						</div>
						
						
						</form>
					</div>`,
			success: function(res) {
				$('.sum').on('click',function(){
					layer.close(index)
				})
				$('.close').on('click',function(){
					layer.close(index)
				})
			}
		});

	}
})